/*
 * @Date: 2022-11-24 09:53:32
 * @LastEditors: sailornpg
 * @LastEditTime: 2022-11-29 14:19:19
 * @FilePath: \vue-admin-ts\src\store\main\system\index.ts
 * @Description: system
 */
import { Module } from "vuex";
import type { IRootState } from "@/store/type";
import { getPageListData } from "@/apis/system";
import { ISysteamState } from "./type";

const systemModule: Module<ISysteamState, IRootState> = {
  namespaced: true,
  state: () => {
    return {
      usersList: [],
      usersCount: 0,
      roleCount: 0,
      roleList: [],
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0,
    };
  },
  actions: {
    async getPageListAction({ commit }, payload: any) {
      console.log(payload);

      const pageName = payload.pageName;
      const pageUrl = `/${pageName}/list`;
      const res = await getPageListData(pageUrl, payload.queryInfo);
      const { list, totalCount } = res.data;
      const changePageName =
        pageName.slice(0, 1).toUpperCase() + pageName.slice(1);
      commit(`change${changePageName}List`, list);
      commit(`change${changePageName}Count`, totalCount);
    },
  },
  mutations: {
    changeUsersList(state, payload) {
      state.usersList = payload;
    },
    changeUsersCount(state, payload) {
      state.usersCount = payload;
    },
    changeRoleList(state, payload) {
      state.roleList = payload;
    },
    changeRoleCount(state, payload) {
      state.roleCount = payload;
    },
    changeGoodsList(state, payload) {
      state.goodsList = payload;
    },
    changeGoodsCount(state, payload) {
      state.goodsCount = payload;
    },
    changeMenuList(state, payload) {
      state.menuList = payload;
    },
    changeMenuCount(state, payload) {
      state.menuCount = payload;
    },
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`];
      };
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`];
      };
    },
  },
};
export default systemModule;
